const mysql = require('mysql');

const pool = mysql.createPool({
    host: "192.168.0.51",
    user: "root",
    password: "Zhj123456",
    port: "3306",
    database: "lis"
});

pool.on('enqueue', () => {
    //logger.info('Waiting for available connection slot');
});

module.exports.Pool = pool;

module.exports.getConnection = (cb) => {
    // 判断cb是否为函数
    if (typeof cb == "function") {
        // 调用pool的getConnection方法，传入回调函数
        pool.getConnection(function(err, connection) {
            // 执行回调函数，传入err和connection
            cb(err, connection);
        });
    } else {
        // 返回一个Promise对象
        return new Promise((resolve, reject) => {
            // 调用pool的getConnection方法，传入回调函数
            pool.getConnection((err, connection) => {
                // 判断err是否存在
                if (err) {
                    // 如果存在，则执行reject方法，传入err
                    reject(err);
                } else {
                    // 如果不存在，则执行resolve方法，传入connection
                    resolve(connection);
                }
            });
        });
    }
};
module.exports.query = (sql, values, cb) => {
    // 判断cb是否为函数
    if (typeof cb == "function") {
        // 获取连接
        pool.getConnection((err, connection) => {
            // 如果有错误，释放连接，并执行回调函数，传入错误信息
            if (err) {
                connection.release()
                cb(err)
            } else {
                // 执行查询语句，传入sql语句和值，并执行回调函数，传入错误信息和查询结果
                connection.query(sql, values, (error, rows) => {
                    connection.release()
                    cb(error, rows)
                })
            }
        })
    } else {
        // 返回一个Promise对象
        return new Promise((resolve, reject) => {
            // 获取连接
            pool.getConnection((err, connection) => {
                // 如果有错误，释放连接，并执行reject函数，传入错误信息
                if (err) {
                    connection.release()
                    reject(err)
                } else {
                    // 执行查询语句，传入sql语句和值，并执行resolve函数，传入查询结果
                    connection.query(sql, values, (error, rows) => {
                        connection.release()
                        // 如果有错误，执行reject函数，传入错误信息
                        if (error)
                            reject(error)
                        else
                            resolve(rows)
                    })
                }
            })
        })
    }
};
module.exports.exec = (sql, values, cb) => {
    if (typeof cb == "function") {
        pool.getConnection((err, connection) => {
            if (err) {
                connection.release();
                cb(err);
            } else {
                connection.query(sql, values, (error, rows) => {
                    connection.release();
                    cb(error, rows);
                });
            }
        });
    } else {
        return new Promise((resolve, reject) => {
            pool.getConnection((err, connection) => {
                if (err) {
                    connection.release();
                    reject(err);
                } else {
                    connection.query(sql, values, (error, rows) => {
                        connection.release();
                        if (error)
                            reject(error);
                        else
                            resolve(rows);
                    });
                }
            });
        });
    }
};
module.exports.beginTransaction = (connection, cb) => {
    if (typeof cb == "function") {
        connection.beginTransaction(function(err) {
            if (err) {
                throw err;
            }
            cb(null, connection);
        });
    } else {
        return new Promise((resolve, reject) => {
            connection.beginTransaction(function(err) {
                if (err) {
                    reject(err);
                } else {
                    resolve(connection);
                }
            });
        });
    }
};
module.exports.rollback = (connection, cb) => {
    if (typeof cb == "function") {
        connection.rollback(function() {
            connection.release();
            cb && cb();
        });
    } else {
        return new Promise((resolve, reject) => {
            connection.rollback(function(err) {
                connection.release();
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }
};
module.exports.commit = (connection, cb) => {
    if (typeof cb == "function") {
        connection.commit(function(err) {
            if (err) {
                connection.rollback(function() {
                    cb && cb(err);
                    throw err;
                });
            }
            connection.release();
            cb && cb();
        });
    } else {
        return new Promise((resolve, reject) => {
            connection.commit(function(err) {
                if (err) {
                    connection.rollback(function() {
                        reject(err);
                    });
                }
                connection.release();
                resolve();
            });
        });
    }
};
//检查是否链接失败
this.getConnection((err, connection) => {
    if (err) throw err;
    else {
        // logger.info("connected success!");
        connection.release();
    }
});

/**
 * 带事务
 * @param sql
 * @param values
 * @returns {Promise}
 */
module.exports.exec2 = (connection, sql, values, cb) => {
    if (typeof cb == "function") {
        connection.query(sql, values, (error, rows) => {
            cb(error, rows);
        });
    } else {
        return new Promise((resolve, reject) => {
            connection.query(sql, values, (error, rows) => {
                if (error)
                    reject(error);
                else
                    resolve(rows);
            });
        });
    }
};